package streaming.test.session_transmission;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;

import streaming.Call;
import streaming.control.AudioControl;
import streaming.control.VideoControl;

public class CallImpl implements Call{

	private Transmiter audioTransmiter, videoTransmiter;
	private Receiver audioReceiver, videoReceiver;
	private InetAddress address;
	private int[] ports;
	private boolean videoAvailable = false, audioAvailable = false;


	public CallImpl(String remoteAddress, int[] ports) {
		this.ports = ports;
		try {
			this.address = InetAddress.getByName(remoteAddress);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public CallImpl() {
		checkTransmiters();
	}

	private void checkTransmiters(){
		audioTransmiter = new Transmiter(new AudioFormat(AudioFormat.GSM_RTP));
		audioAvailable = (audioTransmiter.getState() != StreamManager.State.FAILED);

		videoTransmiter = new Transmiter(new VideoFormat(VideoFormat.H263_RTP));
		videoAvailable = (videoTransmiter.getState() != StreamManager.State.FAILED);
	}

	public boolean initializeCall(String remoteAddress, int[] ports){
		this.ports = ports;
		try {
			this.address = InetAddress.getByName(remoteAddress);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		}

		audioReceiver = new Receiver(address, this.ports[0], 22222);
		audioReceiver.initialize();
		if(this.ports.length > 1){
			videoReceiver = new Receiver(address, this.ports[1], 22224);
			videoReceiver.initialize();
		}
		
		audioTransmiter.initialize(address, this.ports[0], 22222);
		if(isVideoAvailable()){
			videoTransmiter.initialize(address, this.ports[1], 22224);
		}
		return true;



	}

	public void startCall(){
		if(isAudioAvailable())
			audioTransmiter.start();
		if(isVideoAvailable())
			videoTransmiter.start();
		audioReceiver.start();
		if(this.ports.length > 1){
			videoReceiver.start();
		}
	
	}
	
	public void stopCall(){
		if(isAudioAvailable())
			audioTransmiter.stop();
		if(isVideoAvailable())
			videoTransmiter.stop();
		audioReceiver.stop();
		if(this.ports.length > 1){
			videoReceiver.stop();
		}
	}
	


	public InetAddress getAddress() {
		return address;
	}

	public void setAddress(InetAddress address) {
		this.address = address;
	}

	public int[] getPorts() {
		return ports;
	}

	public void setPorts(int[] ports) {
		this.ports = ports;
	}

	public boolean isVideoAvailable() {
		return videoAvailable;
	}


	public boolean isAudioAvailable() {
		return audioAvailable;
	}
	
	public static void main(String[] args) {
		CallImpl call = new CallImpl();
		System.out.println("video available: " + call.isVideoAvailable());
		System.out.println("audio available: " + call.isAudioAvailable());
		call.initializeCall("192.168.1.100", new int[]{22222, 22224});
//		try {
//			Thread.sleep(10000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		call.startCall();
//		JFrame f = new JFrame("test streaming");
//		f.setBounds(0, 0, 320, 240);
//		f.getContentPane().add(call.videoReceiver.getVisualComponent());
//		f.setVisible(true);
		
	}


	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean initializeAudioCall(InetAddress address, int port,
			int status, boolean isMonitor) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean initializeVideoCall(InetAddress address, int port,
			int status, boolean isMonitor) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isAudioEstablished() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isAudioRequested() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isVideoEstablished() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isVideoRequested() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setAudioEstablished(boolean flag) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setAudioRequested(boolean flag) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setVideoEstablished(boolean flag) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setVideoRequested(boolean flag) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public AudioControl getAudioControl() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public int getAudioStatus(boolean peerAvailable) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public AudioControl getMonitorAudioControl() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public VideoControl getMonitorVideoControl() {
		// TODO Auto-generated method stub
		return null;
	}
	

	
	@Override
	public int getVideoStatus(boolean peerAvailable) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public void hangUpCall() {
		// TODO Auto-generated method stub
		
	}



	@Override
	public streaming.control.VideoControl getVideoControl() {
		// TODO Auto-generated method stub
		return null;
	}
}
